package org.jeecg.modules.demo.partner.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.jeecg.modules.demo.partner.entity.Partner;
import org.jeecg.modules.demo.partner.entity.SysUser;
import org.jeecg.modules.demo.partner.entity.SysUserDepart;
import org.jeecg.modules.demo.partner.entity.SysUserRole;

import java.util.List;

@Mapper
public interface PartnerMapper extends BaseMapper<Partner> {

    @Select("SELECT CASE WHEN EXISTS (SELECT 1 FROM SYS_USER WHERE USERNAME = #{username}) THEN 1 ELSE 0 END AS result FROM dual")
    Boolean checkUserExists(String username);
    @Select("SELECT CASE WHEN EXISTS (SELECT 1 FROM GC_PARTNER WHERE USERNAME = #{username}) THEN 1 ELSE 0 END AS result FROM dual")
    Boolean checkUserExistsPartner(String username);


    @Select("SELECT CASE WHEN EXISTS (SELECT 1 FROM SYS_USER WHERE REALNAME = #{companyName}) THEN 1 ELSE 0 END AS result FROM dual")
    Boolean checkCompanyExists(String companyName);
    @Select("SELECT CASE WHEN EXISTS (SELECT 1 FROM GC_PARTNER WHERE COMPANY_NAME = #{companyName}) THEN 1 ELSE 0 END AS result FROM dual")
    Boolean checkCompanyExistsPartner(String companyName);

    @Select("SELECT * FROM GC_PARTNER WHERE ID = #{id}")
    Partner getPartnerById(String id);

    @Insert("INSERT INTO SYS_USER (ID, USERNAME, REALNAME, PASSWORD, SALT, ORG_CODE, STATUS, DEL_FLAG) VALUES (#{user.id}, #{user.username}, #{user.realname}, #{user.password}, #{user.salt}, #{user.orgCode}, #{user.status}, #{user.delFlag})")
    void insertUser(@Param("user")SysUser user);

    @Insert("INSERT INTO SYS_USER_ROLE (ID,USER_ID, ROLE_ID) VALUES (#{userRole.id}, #{userRole.userId}, #{userRole.roleId})")
    void insertUserRole(@Param("userRole")SysUserRole userRole);

    @Insert("INSERT INTO SYS_USER_DEPART (USER_ID, DEPART_ID) VALUES (#{userDeaprt.userId}, #{userDeaprt.depId})")
    void insertUserDeaprt(@Param("userDeaprt")SysUserDepart userDeaprt);

    @Update("UPDATE GC_PARTNER SET STATUS = 1 WHERE ID = #{id}")
    void updatePartnerById(String id);

//    @Select("SELECT * FROM GC_PARTNER WHERE COOPERATOR_TYPE LIKE '%'#{getCooperatorType}'%'")@
    @Select("${sql}")
    List<Partner> getPartnerByType(String sql);
}
